package org.jeecg.modules.oms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.oms.entity.OmsInquiryRule;
import org.jeecg.modules.oms.entity.OmsInquiryRuleValue;
import org.jeecg.modules.oms.mapper.OmsInquiryRuleMapper;
import org.jeecg.modules.oms.service.IOmsInquiryRuleService;
import org.jeecg.modules.oms.service.IOmsInquiryRuleValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 询单规则服务实现
 */
@Slf4j
@Service
public class OmsInquiryRuleServiceImpl extends ServiceImpl<OmsInquiryRuleMapper, OmsInquiryRule> implements IOmsInquiryRuleService {

    @Autowired
    private IOmsInquiryRuleValueService inquiryRuleValueService;

    @Override
    public IPage<OmsInquiryRule> getInquiryRulePageList(Page<OmsInquiryRule> page, String name, Integer type, Integer enabled) {
        return baseMapper.getInquiryRulePageList(page, name, type, enabled);
    }

    @Override
    public List<OmsInquiryRule> getEnabledRulesByType(Integer type) {
        return baseMapper.getEnabledRulesByType(type);
    }

    @Override
    public boolean updateEnabledStatus(String id, Integer enabled) {
        try {
            LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            String updateBy = loginUser != null ? loginUser.getUsername() : "system";
            
            int result = baseMapper.updateEnabledStatus(id, enabled, updateBy);
            return result > 0;
        } catch (Exception e) {
            log.error("更新规则启用状态失败", e);
            return false;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveRuleWithValues(OmsInquiryRule rule, List<String> values) {
        try {
            LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            String currentUser = loginUser != null ? loginUser.getUsername() : "system";
            Date now = new Date();

            // 保存规则
            if (rule.getId() == null) {
                // 新增
                rule.setId(UUIDGenerator.generate());
                rule.setCreateBy(currentUser);
                rule.setCreateTime(now);
                rule.setDelFlag(0);
                rule.setEnabled(1);
                this.save(rule);
            } else {
                // 更新
                rule.setUpdateBy(currentUser);
                rule.setUpdateTime(now);
                this.updateById(rule);
                
                // 删除原有规则值
                inquiryRuleValueService.deleteByRuleId(rule.getId());
            }

            // 保存规则值
            if (values != null && !values.isEmpty()) {
                inquiryRuleValueService.batchSaveValues(rule.getId(), values);
            }

            return true;
        } catch (Exception e) {
            log.error("保存规则及其值失败", e);
            throw new RuntimeException("保存规则及其值失败: " + e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteRuleWithValues(String id) {
        try {
            // 逻辑删除规则
            OmsInquiryRule rule = this.getById(id);
            if (rule != null) {
                LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
                String currentUser = loginUser != null ? loginUser.getUsername() : "system";
                
                rule.setDelFlag(1);
                rule.setUpdateBy(currentUser);
                rule.setUpdateTime(new Date());
                this.updateById(rule);
                
                // 删除规则值
                inquiryRuleValueService.deleteByRuleId(id);
            }
            
            return true;
        } catch (Exception e) {
            log.error("删除规则及其值失败", e);
            throw new RuntimeException("删除规则及其值失败: " + e.getMessage());
        }
    }
}
